package com.mytool.service.impl;

import com.mytool.model.TableMetaData;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 代码结构生成器，负责生成项目结构描述
 */
@Component
public class CodeStructureGenerator {
    
    /**
     * 根据架构类型生成项目结构
     *
     * @param packagePrefix 包前缀
     * @param tableMetaData 表元数据
     * @param includeTests 是否包含测试代码
     * @param frameworkType 架构类型
     * @return 项目结构描述
     */
    public List<String> generateProjectStructure(String packagePrefix, TableMetaData tableMetaData, boolean includeTests, String frameworkType) {
        if ("SPRING_BOOT".equals(frameworkType)) {
            return generateSpringBootProjectStructure(packagePrefix, tableMetaData, includeTests);
        } else {
            return generateColaProjectStructure(packagePrefix, tableMetaData, includeTests);
        }
    }
    
    /**
     * 生成Spring Boot 3.3.0架构的项目结构
     *
     * @param packagePrefix 包前缀
     * @param tableMetaData 表元数据
     * @param includeTests 是否包含测试代码
     * @return 项目结构描述
     */
    private List<String> generateSpringBootProjectStructure(String packagePrefix, TableMetaData tableMetaData, boolean includeTests) {
        List<String> structure = new ArrayList<>();
        String entity = tableMetaData.getDomainObjectName();
        String tableName = tableMetaData.getTableName();
        
        // 主项目结构
        structure.add(packagePrefix);
        structure.add("├── controller");
        structure.add("│   └── " + entity + "Controller.java");
        structure.add("├── domain");
        structure.add("│   ├── entity");
        structure.add("│   │   └── " + entity + ".java");
        structure.add("│   └── service");
        structure.add("│       ├── " + entity + "Service.java");
        structure.add("│       └── impl");
        structure.add("│           └── " + entity + "ServiceImpl.java");
        structure.add("├── infrastructure");
        structure.add("│   └── repository");
        structure.add("│       └── " + entity + "Repository.java");
        
        // 如果包含测试
        if (includeTests) {
            structure.add("└── test");
            structure.add("    ├── controller");
            structure.add("    │   └── " + entity + "ControllerTest.java");
            structure.add("    ├── service");
            structure.add("    │   └── " + entity + "ServiceTest.java");
            structure.add("    └── repository");
            structure.add("        └── " + entity + "RepositoryTest.java");
        }
        
        return structure;
    }
    
    /**
     * 生成COLA架构的项目结构
     *
     * @param packagePrefix 包前缀
     * @param tableMetaData 表元数据
     * @param includeTests 是否包含测试代码
     * @return 项目结构描述
     */
    private List<String> generateColaProjectStructure(String packagePrefix, TableMetaData tableMetaData, boolean includeTests) {
        List<String> structure = new ArrayList<>();
        String entity = tableMetaData.getDomainObjectName();
        String tableName = tableMetaData.getTableName().toLowerCase();
        
        // COLA架构
        structure.add(packagePrefix);
        structure.add("├── adapter");
        structure.add("│   └── web");
        structure.add("│       └── " + tableName);
        structure.add("│           └── " + entity + "Controller.java");
        structure.add("├── application");
        structure.add("│   ├── executor");
        structure.add("│   │   └── " + tableName);
        structure.add("│   │       ├── " + entity + "AddCmdExe.java");
        structure.add("│   │       ├── " + entity + "ListQryExe.java");
        structure.add("│   │       ├── " + entity + "ModifyCmdExe.java");
        structure.add("│   │       └── " + entity + "GetQryExe.java");
        structure.add("│   ├── service");
        structure.add("│   │   └── impl");
        structure.add("│   │       └── " + entity + "ServiceImpl.java");
        structure.add("│   └── assembler");
        structure.add("│       └── " + entity + "Assembler.java");
        structure.add("├── client");
        structure.add("│   ├── api");
        structure.add("│   │   └── " + entity + "ServiceI.java");
        structure.add("│   ├── dto");
        structure.add("│   │   ├── data");
        structure.add("│   │   │   └── " + entity + "DTO.java");
        structure.add("│   │   ├── command");
        structure.add("│   │   │   ├── " + entity + "AddCmd.java");
        structure.add("│   │   │   └── " + entity + "ModifyCmd.java");
        structure.add("│   │   └── query");
        structure.add("│   │       ├── " + entity + "ListQry.java");
        structure.add("│   │       └── " + entity + "GetQry.java");
        structure.add("├── domain");
        structure.add("│   ├── model");
        structure.add("│   │   └── " + tableName);
        structure.add("│   │       └── " + entity + ".java");
        structure.add("│   ├── gateway");
        structure.add("│   │   └── " + entity + "Gateway.java");
        structure.add("│   └── service");
        structure.add("│       └── " + entity + "DomainService.java");
        structure.add("├── infrastructure");
        structure.add("│   ├── dataobject");
        structure.add("│   │   └── " + entity + "DO.java");
        structure.add("│   ├── mapper");
        structure.add("│   │   └── " + entity + "Mapper.java");
        structure.add("│   ├── converter");
        structure.add("│   │   └── " + entity + "Converter.java");
        structure.add("│   └── repository");
        structure.add("│       └── " + entity + "RepositoryImpl.java");
        structure.add("└── resources");
        structure.add("    └── mapper");
        structure.add("        └── " + entity + "Mapper.xml");
        
        // 如果包含测试
        if (includeTests) {
            structure.add("└── test");
            structure.add("    ├── adapter");
            structure.add("    │   └── " + entity + "ControllerTest.java");
            structure.add("    ├── application");
            structure.add("    │   └── " + entity + "ServiceTest.java");
            structure.add("    ├── domain");
            structure.add("    │   └── " + entity + "DomainServiceTest.java");
            structure.add("    └── infrastructure");
            structure.add("        └── " + entity + "RepositoryTest.java");
        }
        
        return structure;
    }
} 